package labox.innovation.gameserver.network.clientpackets;

import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.L2Party;
import labox.innovation.gameserver.model.L2PartyManager;
import labox.innovation.gameserver.model.PartyMemberExDropped;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * @author ljsh
 * @date 2010-4-8
 * @version 1.0
 * @description 请求离开队伍
 */
public final class RequestOutPartyMember extends L2GameClientPacket {
	private static final String _C__2C_REQUESTOUSTPARTYMEMBER = "[C] 2C RequestOustPartyMember";

	// private static Logger _log =
	// Logger.getLogger(RequestJoinParty.class.getName());

	private long charId;

	@Override
	protected void readImpl() {
		charId = readLong();
	}

	@Override
	protected void runImpl() {
		FuzePcInstance activeChar = getClient().getActiveChar();
		FuzePcInstance target = FuzeWorld.getInstance().getPlayer(charId);//
		L2Party party = null;
		if (activeChar == null)
			return;
		party = activeChar.getParty();
		if (activeChar.isInParty() && party.isLeader(activeChar))// 操作者是队长
		{
			party.removePartyMember(party.getPlayerById(charId));
			if (L2PartyManager.getInstance().getAllOfflinePlayer().containsKey(charId)) //如果被踢的玩家掉线
			{	PartyMemberExDropped  leave=L2PartyManager.getInstance().getAllOfflinePlayer().get(charId);
				if(leave!=null){
					leave.cancel();
					L2PartyManager.getInstance().getAllOfflinePlayer().remove(charId);
				}	
			}
		}
		if (activeChar.isInParty() && !party.isLeader(activeChar))// 操作者不是队长
		{
			if (activeChar == target) {
				party.removePartyMember(target);
			} else {
				activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_NOT_THE_LEADER_OF_THE_PARTY));
			}
		}
	}

	@Override
	public String getType() {
		return _C__2C_REQUESTOUSTPARTYMEMBER;
	}
}
